package org.kdev.wtf.adapter;

import static org.kdev.wtf.WtfManager.adapter;

import com.thoughtworks.selenium.SeleniumException;

public class Ext4Adapter
{
   public static final String TAK = "Yes";
   public static final String NIE = "No";
   private static final String UPDATE_INDICATOR_TEXT = "Loading...";
   private static final String DATA_LOAD_TEXT = "Wczytywanie danych...";

   public static void waitForLoading()
   {
      String seleniumCondition;
      String elementPleaseWait = "xpath=//div[@class='x-mask-msg x-mask-loading']";

      if (adapter.isElementPresent(elementPleaseWait))
      {
         seleniumCondition = "!selenium.isVisible(\"" + elementPleaseWait + "\")";
         adapter.waitForCondition(seleniumCondition);
      }

      seleniumCondition = "!selenium.isTextPresent('" + UPDATE_INDICATOR_TEXT + "')";
      adapter.waitForCondition(seleniumCondition);

      elementPleaseWait = "xpath=//div[@class='x-mask-loading']";
      if (adapter.isElementPresent(elementPleaseWait))
      {
         seleniumCondition = "!selenium.isVisible(\"" + elementPleaseWait + "\")";
         adapter.waitForCondition(seleniumCondition);
      }

      seleniumCondition = "!selenium.isTextPresent('" + DATA_LOAD_TEXT + "')";
      adapter.waitForCondition(seleniumCondition);
   }

   public static boolean isElementPresent(String string)
   {
      String jscript = "window.Ext.getCmp('komunikatForm');";
      String scriptEval = adapter.getEval(jscript);

      if (scriptEval.isEmpty() || scriptEval == null)
         return false;
      else
         return true;
   }

   public static void waitForWindow(String windowId)
   {
      try
      {
         adapter.waitForElement(windowId);
      }
      catch (SeleniumException e)
      {
         throw new SeleniumException("Can not open window: '" + windowId + "'", e);
      }
   }

   public static void clickButtonByXPath(String button)
   {
      String xpath = "xpath=//button/span[text()='" + button + "']";
      adapter.click(xpath);
   }

   public static void clickButtonById(String buttonId)
   {
      String jscript = "window.Ext.getCmp('" + buttonId + "').fireEvent('click')";
      adapter.getEval(jscript);
   }

   public static void clickBoldText(String text)
   {
      String xpath = "xpath=//div/b[text()='" + text + "']";
      adapter.waitForElement(xpath);
      adapter.click(xpath);
   }

   public static void setValueInElement(String elId, String value)
   {
      adapter.getEval("window.Ext.getCmp('" + elId + "').setValue('" + value + "');");
   }

   public static void clickBoldTextInGrid(String grid, String value)
   {
      String xpath = "xpath=//div[@id='" + grid + "']//div/b[text()='" + value + "']";
      adapter.waitForElement(xpath);
      adapter.click(xpath);
   }

   public static void selectById(String combo, String id)
   {
      String jscript = "window.Ext.getCmp('" + combo + "').select(" + id + ")";
      adapter.getEval(jscript);
   }

   public static void highlightGridRow(String grid, int rowNumber)
   {
      String script = "var grid = window.Ext.getCmp('" + grid + "');\n";
      script += "var selection = grid.getSelectionModel().selectRow(" + rowNumber + ");";
      adapter.getEval(script);
   }

   public static void highlightTreeNodeByNazwa(String grid, String nazwa)
   {
      String clickLoad = "var rec = window.Ext.getCmp('" + grid
            + "').getStore().getRootNode().findChild('nazwa','" + nazwa + "');";
      clickLoad += "var view = window.Ext.getCmp('" + grid + "').getView();";
      clickLoad += "view.fireEvent('itemclick', view, rec, null, 0);";
      adapter.getEval(clickLoad);

      String highlight = "var rec = window.Ext.getCmp('" + grid
            + "').getStore().getRootNode().findChild('nazwa','" + nazwa + "');";
      highlight += "window.Ext.getCmp('" + grid + "').getSelectionModel().selectRange(rec,rec);";
      adapter.getEval(highlight);
   }

   public static void highlightTreeNodeByField(String grid, String field, String nazwa)
   {
      String clickLoad = "var rec = window.Ext.getCmp('" + grid
            + "').getStore().getRootNode().findChild('" + field + "','" + nazwa + "');";
      clickLoad += "var view = window.Ext.getCmp('" + grid + "').getView();";
      clickLoad += "view.fireEvent('itemclick', view, rec, null, 0);";
      adapter.getEval(clickLoad);

      String highlight = "var rec = window.Ext.getCmp('" + grid
            + "').getStore().getRootNode().findChild('" + field + "','" + nazwa + "');";
      highlight += "window.Ext.getCmp('" + grid + "').getSelectionModel().selectRange(rec,rec);";
      adapter.getEval(highlight);
   }

   public static boolean checkIfNodeExist(String grid, String nazwa)
   {
      String script = "window.Ext.getCmp('" + grid
            + "').getStore().getRootNode().findChild('nazwa','" + nazwa + "');";
      String s = adapter.getEval(script);

      if (s.isEmpty() || s == null)
         return false;
      else
         return true;
   }
}